#include "Regwindow.h"
#include "ui_Regwindow.h"

RegWindow::RegWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("注册界面");
    this->resize(798,646);
}

RegWindow::~RegWindow()
{
    delete ui;
}

//显示注册界面
void RegWindow::replyShowReg(){
    this->show();
}

//返回按钮
void RegWindow::on_reg_back_btn_clicked()
{
    emit backToLoginWin();
    this->hide();
}

//注册按钮
void RegWindow::on_reg_reg_btn_clicked()
{
    //首先判断账号是否在数据库已经存在了
    bool flag = false;  //存在为1,不存在为0
    QString warning = "";    //提示信息
    UserTable user_table;
    User *u = new User[500];
    u = user_table.queryAllUser();
//    memcpy(u,user_table.queryAllUser(),sizeof(u));

    int i=0;
    while(u[i].name != ""){
//        qDebug()<<u[i].name;
//        qDebug()<<u[i].account;
//        qDebug()<<u[i].phone_num;
        if(u[i].account == ui->re_uid_et->toPlainText()){
            flag = true;
            warning += "当前账户已经存在了";
            break;
        }
        i++;
    }

    //判断一下两个密码是否相等,不相等的话依然让flag=true
    if(ui->re_password_et_1->toPlainText() != ui->re_password_et_2->toPlainText()){
        warning = "";
        warning += "两次密码不一致!";
        flag = true;
    }

    //设置警告信息
    ui->reg_warning_text->setText(warning);

    if(!flag){
        //如果没有任何问题的话,直接向数据库中插入该用户
        User new_user;
        QString account = ui->re_uid_et->toPlainText();
        QString password = ui->re_password_et_1->toPlainText();
        QString name = "默认用户";
        QString phoneNum = "123";
        int category = 0;   //注册的账户默认是用户
        new_user = User(name,category,account,password,phoneNum);

        user_table.insertUser(new_user);

        //发出信号,回到登陆界面
        emit backToLoginWin();
        this->hide();
    }
}
